package com.peter.design.pattern.singleton.lazy;

/**
 * @Auther: 肖冬
 * @Date: 2020/7/24 15:34
 * @Description: 双重检查锁定 这是个错误的优化，原因在于，new对象过程中的指令重排序，所以需要加volatile关键字，
 * 来禁止这种重排序，但是volatile语法有版本限制jdk1.5+
 */
public class LazyDoubleCheckSingleton {
    private volatile static LazyDoubleCheckSingleton lazy = null;

    private LazyDoubleCheckSingleton(){}

    public static LazyDoubleCheckSingleton getInstance(){
        if(lazy == null){
            synchronized (LazyDoubleCheckSingleton.class){
                if(lazy == null){
                    //1.分配内存给这个对象
                    //2.初始化对象
                    //3.设置lazy指向刚分配的内存地址
                    lazy = new LazyDoubleCheckSingleton();
                }
            }
        }
        return lazy;
    }
}
